package com.mitcub.panda.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mitcub.panda.base.BaseController;
import com.mitcub.panda.entity.Admin;
import com.mitcub.panda.entity.Dept;
import com.mitcub.panda.entity.Role;
import com.mitcub.panda.mapper.DeptMapper;
import com.mitcub.panda.mapper.RoleMapper;
import com.mitcub.panda.query.AdminQuery;
import com.mitcub.panda.service.IAdminService;
import com.mitcub.panda.utils.RespResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @项目名称: panda
 * @类名称: SystemUserController
 * @类描述:
 * @作者: tangloong
 * @创建时间: 2021/11/9 14:45
 */
@Controller
@RequestMapping("/system/user")
public class SystemUserController extends BaseController {
    @Autowired
    private IAdminService adminService;

    @Resource
    private RoleMapper roleMapper;
    @Resource
    private DeptMapper deptMapper;

    @ResponseBody
    @RequestMapping("/list")
    public RespResult list(AdminQuery query) {
        return adminService.getList(query);
    }

    /**
     * 添加记录
     *
     * @param entity 实体对象
     * @return
     */
    @ResponseBody
    @PostMapping("/add")
    public RespResult add(@RequestBody Admin entity) {
        return adminService.edit(entity);
    }

    /**
     * 修改记录
     *
     * @param entity 实体对象
     * @return
     */
    @ResponseBody
    @PostMapping("/update")
    public RespResult update(@RequestBody Admin entity) {
        return adminService.edit(entity);
    }

    /**
     * 获取记录详情
     *
     * @param id    记录ID
     * @param model 模型
     * @return
     */
    @Override
    public String edit(Long id, Model model) {
        Map<String, Object> info = new HashMap<>();
        if (id != null && id > 0) {
            info = adminService.info(id);
        }
        List<Role> roles = roleMapper.selectList(new QueryWrapper<Role>().select("name","id"));
        List<Dept> depts = deptMapper.selectList(new QueryWrapper<Dept>().select("id,name"));
        model.addAttribute("roles", roles);
        model.addAttribute("depts", depts);
        model.addAttribute("info", info);
        return super.edit(id, model);
    }

    /**
     * 删除记录
     *
     * @param id 记录ID
     * @return
     */
    @ResponseBody
    @GetMapping("/delete/{id}")
    public RespResult delete(@PathVariable("id") Long id) {
        return adminService.deleteById(id);
    }

    /**
     * 批量删除
     *
     * @param ids 记录ID(多个使用逗号","分隔)
     * @return
     */
    @ResponseBody
    @GetMapping("/batchDelete/{ids}")
    public RespResult batchDelete(@PathVariable("ids") String ids) {
        return adminService.deleteByIds(ids);
    }

    /**
     * 设置状态
     * @param entity 实体对象
     * @return
     */
    @ResponseBody
    @PostMapping("/setStatus")
    public RespResult setStatus(@RequestBody Admin entity) {
        Admin admin = new Admin();
        admin.setId(entity.getId());
        admin.setStatus(entity.getStatus());
        return adminService.setStatus(admin);
    }
}
